﻿using Core.SimpleTemp.Common;
using Core.SimpleTemp.Common.PagingQuery;
using Core.SimpleTemp.Entitys;
using Core.SimpleTemp.Repositories;
using Core.Toolkit.Common;
using Core.Toolkit.Core;
using Core.Toolkit.Core.Api;
using Core.Toolkit.Core.Application;
using Microsoft.EntityFrameworkCore;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Core.SimpleTemp.Application
{
    [AutoDi()]
    public class JG_DutiesPlanService : BaseAppService<JG_DutiesPlanDto, JG_DutiesPlan, JG_DutiesPlanRepository>
    {
        readonly ExcelHelp _excelHelp;
        public JG_DutiesPlanService(JG_DutiesPlanRepository repository, ExcelHelp excelHelp) : base(repository)
        {
            _excelHelp = excelHelp;
        }



        private LoadPageOffsetModelAuto<JG_DutiesPlan, JG_DutiesPlanDto>
            GetLoadPageOffsetMode(PagingModel<JG_DutiesPlanDto>
                pagingQueryModel)
        {
            var loadPageOffsetModel = this.GetLoadPageOffsetModelAuto(pagingQueryModel);
            var baseQuery = _repository.QueryBase().AsNoTracking();
            loadPageOffsetModel.QueryWhere = this.CreateQRList()

     .Add("Year", "=") //年份
     //.Add(nameof(JG_DutiesPlanDto.AssessmentObject), "=") //评估对象
     .Add("AssessmentID", "=") //
     .Add("AssessmentEnd", "=") //
     .Add("AssessmentDate", "=") //
     .Add("AssessmentContent", "=") //
     .Add("ResponsibleDeptID", "=") //
     .Add("ResponsibleUserID", "=") //
     .Add("AssessmentMethod", "=") //
     .Add("AssessmentResult", "=") //
     .Add("AttachmentId", "=") //
     .Add("AttachmentName", "=") //
     .Add("CreateFactory", "=") //
     .Add("CreateDept", "=") //
     .Add("UpdateDept", "=");//


            loadPageOffsetModel.BasqQuery = baseQuery;
            loadPageOffsetModel.Order = orderModel => orderModel.CreateTime;
            loadPageOffsetModel.Selector = model => new JG_DutiesPlan()
            {
                Id = model.Id,
                Year = model.Year,//
                AssessmentID = model.AssessmentID,//
                AssessmentEnd = model.AssessmentEnd,//
                AssessmentDate = model.AssessmentDate,//
                AssessmentContent = model.AssessmentContent,//
                AssessmentObject = model.AssessmentObject,//
                ResponsibleDeptID = model.ResponsibleDeptID,//
                ResponsibleUserID = model.ResponsibleUserID,//
                AssessmentMethod = model.AssessmentMethod,//
                AssessmentResult = model.AssessmentResult,//
                AttachmentId = model.AttachmentId,//
                AttachmentName = model.AttachmentName,//
                CreateFactory = model.CreateFactory,//
                CreateDept = model.CreateDept,//
                UpdateDept = model.UpdateDept,//
            };
            return loadPageOffsetModel;
        }


        public async Task<IPageModel<JG_DutiesPlanDto>> GetList(PagingModel<JG_DutiesPlanDto>
            pagingQueryModel)
        {
            var loadPageOffsetModel = GetLoadPageOffsetMode(pagingQueryModel);
            return await base.LoadPageOffsetAsync(loadPageOffsetModel);
        }


        public async Task<string>
            Export(PagingModel<JG_DutiesPlanDto>
                pagingQueryModel)
        {
            var loadPageOffsetModel = GetLoadPageOffsetMode(pagingQueryModel);
            var DataList = await this.ExportDataAsync(loadPageOffsetModel);
            var excelData = DataList.Select(model => new
            {
                model.Year,
                Year_title = "",
                model.AssessmentID,
                AssessmentID_title = "",
                model.AssessmentEnd,
                AssessmentEnd_title = "",
                model.AssessmentDate,
                AssessmentDate_title = "",
                model.AssessmentContent,
                AssessmentContent_title = "",
                model.AssessmentObject,
                AssessmentObject_title = "",
                model.ResponsibleDeptID,
                ResponsibleDeptID_title = "",
                model.ResponsibleUserID,
                ResponsibleUserID_title = "",
                model.AssessmentMethod,
                AssessmentMethod_title = "",
                model.AssessmentResult,
                AssessmentResult_title = "",
                model.AttachmentId,
                AttachmentId_title = "",
                model.AttachmentName,
                AttachmentName_title = "",
                model.CreateFactory,
                CreateFactory_title = "",
                model.CreateDept,
                CreateDept_title = "",
                model.UpdateDept,
                UpdateDept_title = "",
            }).ToList();




            return _excelHelp.Create(excelData);
        }

    }

}
